/**
 * @author daemach
 */
cssMonkey = function(root){
	this.name = "cssMonkey";
	this.version = "1.2";
	this.root = root;
	this.sheets = document.styleSheets;
};

jQuery.extend(cssMonkey,{
	prototype: {
		getSheets: function(){
			var media, mediaType, ss=[];
			if (document.styleSheets.length > 0) {
		        for (var i = 0; i < document.styleSheets.length; i++) {
		            if (document.styleSheets[i].disabled){
						continue;
					}
		            media = document.styleSheets[i].media;
		            mediaType = typeof media;
					
		            if (mediaType == "string") {
		                if (media == "" || media.indexOf("screen") !=  - 1) {
		                    styleSheet = document.styleSheets[i];
		                }
		            } else if (mediaType == "object") {
		                if (media.mediaText == "" || media.mediaText.indexOf("screen") !=  - 1) {
		                    styleSheet = document.styleSheets[i];
		                }
		            }
		            if (typeof styleSheet != "undefined"){
						ss.push(styleSheet);
					}
		        }
		    }
			return ss;
		},
		getRule: function(ss,s,a){
			var rules,matches=[], sObj=[];
			ss = (ss === null)? getSheets(): [document.styleSheets[ss]];
			for (var i=ss.length-1;i>=0;i--){
				rules = (ss[i].cssRules) ? ss[i].cssRules : ss[i].rules;
				if (!rules.length){
					return matches;
				}
				s = s.toLowerCase();
			    for(var r = rules.length-1; r >= 0; r--) {
			        if (rules[r].selectorText.toLowerCase() == s){
						a = (typeof a == "undefined")? null : rules[r].style[this._camelCase(a)];
						matches.push([rules[r],i,r,a]);
					}
			    }
			}
			return matches;
		},
		addSheet: function() {
            var ss = document.createElement("style");
            ss.type = "text/css";
            document.getElementsByTagName("head")[0].appendChild(ss);
			
			return document.styleSheets.length-1;
		},
		toggleSheet: function(disable,id) {
			var s = this.sheets;
			id = (typeof id === "undefined")? null : id;
			disable = (typeof disable === "undefined")? null : disable;
			
			for (var i=0;i<s.length;i++){
				if (id === null || i == id || s[i].href.indexOf(id) >= 0){
					s[i].disabled = (disable || (disable === null && !s[i].disabled))? true : false;
				}
			}
		},
		_camelCase: function(s){
			if (s == "float"){ return "cssFloat"; };
			for(var str=/-([a-z])/; str.test(s); s=s.replace(str,RegExp.$1.toUpperCase()));
			return s;
		},
		getComputedStyle: function(ele,attr){
			attr = this.camelCase(attr);
			if (ele.currentStyle){
				return ele.currentStyle[attr];
			} else if (window.getComputedStyle){
				return window.getComputedStyle(ele,null)[attr];
			}
			return null;
		},
		getRootStyle: function(ele,attr){
			attr = this.camelCase(attr);
			var tmpAttr = this.getComputedStyle(ele,attr),tmpEle = ele;
			if (attr.indexOf("Color")>=0){
				tmpAttr = this.root.cw.rgb2hex(tmpAttr);
				while (tmpEle !== "body" && tmpAttr == "transparent"){
					tmpEle = tmpEle.parentNode;
					tmpAttr = this.root.cw.rgb2hex(this.getComputedStyle(tmpEle,attr));
				}
				tmpAttr = (tmpAttr == "transparent")? "000000" : tmpAttr;
			}
			return tmpAttr;
		},
		setRule: function(ss,selector,styles,index){
			var rules = this.getRule(ss,selector), s,v,t;
			if (rules.length){
				styles = styles.split(";");
				if (!styles[styles.length-1].trim().length){ styles.pop(); }
				for (var i=0;i<rules.length;i++){
					for (var x=0;x<styles.length;x++){
						t = styles.split(":")
						s = this._camelCase(t[0].trim());
						v = t[1].trim();
						rules[i][0].style[s] = v;
					}
					return i
				}
			} else {
				var sheet = this.sheets[this.sheets.length-1];
				var rules = sheet.cssRules ? sheet.cssRules : sheet.rules;
			    if (index == undefined) {
			        index = rules.length;
			    }
				if (sheet.insertRule){
					sheet.insertRule(selector + "{" + styles + "}", index);
				} else if (sheet.addRule){
					sheet.addRule(selector, styles, index);
				}
				return rules.length-1;
			}
		},
		deleteRule: function (s){
			var rules = this.getRule(s),sheet;
		    for(var i = rules.length-1; i >= 0; i--) {
				sheet = this.sheets[rules[i][1]];
			    if (sheet.deleteRule){
					sheet.deleteRule(rules[i][2]);
				} else if (sheet.removeRule){
					sheet.removeRule(rules[i][2]);
				} 
		    }
		}
	}
});
cssMonkey = function(root){
	this.name = "cssMonkey";
	this.version = "1.2";
	this.root = root;
	this.sheets = [];
	if (document.styleSheets){
		this.parseStyles();
	} else {
		return false;
	}
};

jQuery.extend(cssMonkey,{
	prototype: {
		parseStyles: function(){
			var media, mediaType;
			if (document.styleSheets.length > 0) {
		        for (var i = 0; i < document.styleSheets.length; i++) {
		            if (document.styleSheets[i].disabled){
						continue;
					}
		            media = document.styleSheets[i].media;
		            mediaType = typeof media;
					
		            if (mediaType == "string") {
		                if (media == "" || media.indexOf("screen") !=  - 1) {
		                    styleSheet = document.styleSheets[i];
		                }
		            } else if (mediaType == "object") {
		                if (media.mediaText == "" || media.mediaText.indexOf("screen") !=  - 1) {
		                    styleSheet = document.styleSheets[i];
		                }
		            }
		            if (typeof styleSheet != "undefined"){
						this.sheets.push(styleSheet);
					}
		        }
		    }
		},
		toggleSheet: function(disable,id) {
			var s = this.sheets;
			id = (typeof id === "undefined")? null : id;
			disable = (typeof disable === "undefined")? null : disable;
			
			for (var i=0;i<s.length;i++){
				if (id === null || i == id || s[i].href.indexOf(id) >= 0){
					s[i].disabled = (disable || (disable === null && !s[i].disabled))? true : false;
				}
			}
		},
		getRule: function(s,a){
			var rules,matches=[], sObj=[];
			for (var i=this.sheets.length-1;i>=0;i--){
				rules = (this.sheets[i].cssRules) ? this.sheets[i].cssRules : this.sheets[i].rules;
				if (!rules.length){
					return matches;
				}
				s = s.toLowerCase();
			    for(var r = rules.length-1; r >= 0; r--) {
			        if (rules[r].selectorText.toLowerCase() == s){
						a = (typeof a == "undefined")? null : rules[r].style[this.camelCase(a)];
						matches.push([rules[r],i,r,a]);
					}
			    }
			}
			return matches;
		},
		_findStyle: function(cssText,attr){
			var n = (attr.indexOf(":")>=0) ? attr.split(":")[0].trim() : attr;
			if (n.length){
				for (var i=0;i<cssText.length;i++){
					if (cssText[i].indexOf(n) >= 0){ return i; }
				}			
			}
			return -1;
		},
		camelCase: function(s){
			if (s == "float"){ return "cssFloat"; };
			for(var str=/-([a-z])/; str.test(s); s=s.replace(str,RegExp.$1.toUpperCase()));
			return s;
		},
		getComputedStyle: function(ele,attr){
			attr = this.camelCase(attr);
			if (ele.currentStyle){
				return ele.currentStyle[attr];
			} else if (window.getComputedStyle){
				return window.getComputedStyle(ele,null)[attr];
			}
			return null;
		},
		getRootStyle: function(ele,attr){
			attr = this.camelCase(attr);
			var tmpAttr = this.getComputedStyle(ele,attr),tmpEle = ele;
			if (attr.indexOf("Color")>=0){
				tmpAttr = this.root.cw.rgb2hex(tmpAttr);
				while (tmpEle !== "body" && tmpAttr == "transparent"){
					tmpEle = tmpEle.parentNode;
					tmpAttr = this.root.cw.rgb2hex(this.getComputedStyle(tmpEle,attr));
				}
				tmpAttr = (tmpAttr == "transparent")? "000000" : tmpAttr;
			}
			return tmpAttr;
		},
		setRule: function(selector,styles,index){

			var rules = this.getRule(selector), oCSS, nCSS, o, s, rCSS;
			if (rules.length){
				nCSS = styles.split(";");
				if (!nCSS[nCSS.length-1].trim().length){ nCSS.pop(); }
				for (var i=0;i<rules.length;i++){
					oCSS = rules[i][0].style.cssText.split(";");
					for (var x=0;x<nCSS.length;x++){
						s = this._findStyle(oCSS,nCSS[x]);
						if (s >= 0){
							oCSS[s] = nCSS[x];
						} else {
							oCSS.push(nCSS[x]);
						}
					}
					rules[i][0].style.cssText = oCSS.join(";");
				}
			} else {
				var sheet = this.sheets[this.sheets.length-1];
				var rules = sheet.cssRules ? sheet.cssRules : sheet.rules;
			    if (index == undefined) {
			        index = rules.length;
			    }
				this._insertRule(sheet, selector,styles,index)
			}
		},
		_insertRule: function(sheet, selector,styles,index){

				if (sheet.insertRule){
					sheet.insertRule(selector + "{" + styles + "}", index);
				} else if (sheet.addRule){
					sheet.addRule(selector, styles, index);
				}
		},
		deleteRule: function (s){
			var rules = this.getRule(s),sheet;
		    for(var i = rules.length-1; i >= 0; i--) {
				sheet = this.sheets[rules[i][1]];
			    if (sheet.deleteRule){
					sheet.deleteRule(rules[i][2]);
				} else if (sheet.removeRule){
					sheet.removeRule(rules[i][2]);
				} 
		    }
		}
	}
});

// light it up...
var cssMonkey = new cssMonkey();

